/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import po.Organization;
import myUtils.Constants;
import myUtils.page.*;
import po.User;
import po.Userorgrelation;

/**
 *
 * @author luyang
 */
public class OrganizationDaoImpl extends HibernateDaoSupport implements OrganizationDao
{

    public void addOrg(Organization org)
    {
        this.getHibernateTemplate().saveOrUpdate(org);
    }

    public void deleteOrg(int id)
    {
        Object p = getHibernateTemplate().load(Organization.class, id);
        this.getHibernateTemplate().delete(p);
    }

    public void updateOrg(Organization org)
    {
        this.getHibernateTemplate().saveOrUpdate(org);
    }

    public Organization getOrg(int id)
    {
        return (Organization)this.getHibernateTemplate().get(Organization.class, id);
    }

    public Result getCheckedOrg(int page)
    {
         String queryString="from Organization where type="+Constants.CHECKED_ORG;
         //List tempList=this.getHibernateTemplate().find(queryString);
         
         //return new Result(page,tempList);
         return new Result(page,this,queryString);
    }

    public Result getUncheckedOrg(int page)
    {
         String queryString="from Organization where type="+Constants.UNCHECKED_ORG;
         //List tempList=this.getHibernateTemplate().find(queryString);
         //return new Result(page,tempList);
         return new Result(page,this,queryString);
    }

    public Result getMembers(int id,int page)
    {
        String queryString="from User as usr where usr.id in (select  new list(uor.user.id) from Userorgrelation as uor where uor.organization.id="+id+" and uor.state !="+Constants.ORG_MANAGER+" and uor.organization.type!="+Constants.REJECTED_ORG+")";
        //String queryString="";
        //List tempList=getHibernateTemplate().find(queryString);
        //return new Result(page,tempList);
        return new Result(page,this,queryString);
    }

     public Result getUncheckedMembers(int id,int page)
    {
        String queryString="from User as usr where usr.id in (select  new list(uor.user.id) from Userorgrelation as uor where uor.organization.id="+id+"and uor.state="+Constants.GUEST+" and uor.organization.type!="+Constants.REJECTED_ORG+")";
        //List tempList=getHibernateTemplate().find(queryString);
        //return new Result(page,tempList);
        return new Result(page,this,queryString);
    }

      public Result getCheckedMembers(int id,int page)
    {
        String queryString="from User as usr where usr.id in (select  new list(uor.user.id) from Userorgrelation as uor where uor.organization.id="+id+" and uor.state  ="+Constants.ORG_MEMBER+" and uor.organization.type!="+Constants.REJECTED_ORG+")";
        //List tempList=getHibernateTemplate().find(queryString);
        //return new Result(page,tempList);
        return new Result(page,this,queryString);
    }

    public User getManagerOfOrg(int id)
    {
        String queryString="from User as usr where usr.id in (select new list(uor.user.id) from Userorgrelation as uor where uor.state="+Constants.ORG_MANAGER+" and uor.organization.id="+id+")";
        //String queryString="Select new User()";
        List tempList=getHibernateTemplate().find(queryString);
        User user = null;
        if(tempList.size()>0)
            user=(User) tempList.get(0);
        return user;
    }

    public boolean isLeader(int userId, int orgId) {
         String queryString="from Userorgrelation where state="+Constants.ORG_MANAGER+" and user.id="+userId+" and organization.id="+orgId;
         return !getHibernateTemplate().find(queryString).isEmpty();
    }

    public void deleteMemFromOrg(int userId, int orgId) 
    {
        String queryString="from Userorgrelation where user.id="+userId+" and organization.id="+orgId;
        List tempList=this.getHibernateTemplate().find(queryString);
        for(int i=0;i<tempList.size();++i)
            this.getHibernateTemplate().delete(tempList.get(i));
    }

    public void approveUser(Userorgrelation userorgrelation)
    {
        this.getHibernateTemplate().saveOrUpdate(userorgrelation);
    }

    public boolean isMember(int userId, int orgId)
    {
        String queryString="from Userorgrelation where state!="+Constants.GUEST+" and user.id="+userId+" and organization.id="+orgId;
         return !getHibernateTemplate().find(queryString).isEmpty();
    }

}
